SPB Git forge

spb/immbot-ai

Public
1commits 1branches 0releases
1.5 MBsize
maindefault branch
20 days agolast push
TypeScript 98.3% CSS 0.9% Shell 0.7%
2.5 KB · 65 lines typescript
Raw Blame History
1import { NextResponse } from "next/server";2import { z } from "zod";3import { apiError, parseBody } from "@/lib/api.ts";4import { assertSameOrigin, requireUser } from "@/lib/auth/session.ts";5import { all, get, run } from "@/lib/db/index.ts";67async function owned(id: number, userId: number) {8  const conv = get<{ id: number }>("SELECT id FROM conversations WHERE id = ? AND user_id = ?", id, userId);9  if (!conv) throw Object.assign(new Error("Conversation introuvable."), { status: 404 });10  return conv;11}1213export async function GET(_req: Request, ctx: { params: Promise<{ id: string }> }) {14  try {15    const user = await requireUser();16    const id = parseInt((await ctx.params).id, 10);17    await owned(id, user.id);18    const conversation = get("SELECT * FROM conversations WHERE id = ?", id);19    const messages = all(20      "SELECT id, role, content, citations, attachments, tool_trace, model, mode, knowledge_mode, feedback, flagged, saved, created_at FROM messages WHERE conversation_id = ? ORDER BY id",21      id22    );23    return NextResponse.json({ conversation, messages });24  } catch (e) {25    return apiError(e);26  }27}2829const patchSchema = z.object({30  title: z.string().min(1).max(200).optional(),31  folder: z.string().max(100).optional(),32  pinned: z.boolean().optional(),33  archived: z.boolean().optional(),34});3536export async function PATCH(req: Request, ctx: { params: Promise<{ id: string }> }) {37  try {38    await assertSameOrigin();39    const user = await requireUser();40    const id = parseInt((await ctx.params).id, 10);41    await owned(id, user.id);42    const body = await parseBody(req, patchSchema);43    if (body.title !== undefined) run("UPDATE conversations SET title = ? WHERE id = ?", body.title, id);44    if (body.folder !== undefined) run("UPDATE conversations SET folder = ? WHERE id = ?", body.folder, id);45    if (body.pinned !== undefined) run("UPDATE conversations SET pinned = ? WHERE id = ?", body.pinned ? 1 : 0, id);46    if (body.archived !== undefined) run("UPDATE conversations SET archived = ? WHERE id = ?", body.archived ? 1 : 0, id);47    return NextResponse.json({ ok: true });48  } catch (e) {49    return apiError(e);50  }51}5253export async function DELETE(_req: Request, ctx: { params: Promise<{ id: string }> }) {54  try {55    await assertSameOrigin();56    const user = await requireUser();57    const id = parseInt((await ctx.params).id, 10);58    await owned(id, user.id);59    run("DELETE FROM conversations WHERE id = ?", id);60    return NextResponse.json({ ok: true });61  } catch (e) {62    return apiError(e);63  }64}65